package com.tangkun.environment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.xinwen.TypeReturn;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: XinwenTypeAdapter
 * @Description: 新闻分类适配器
 * @date 2016-08-20 上午11:57:36
 */
public class XinwenTypeAdapter extends BaseAdapter {

    private Context context;
    private List<TypeReturn.DataBean> listData = new ArrayList<TypeReturn.DataBean>();

    public XinwenTypeAdapter(Context context, List<TypeReturn.DataBean> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_xinwen_type, null);
            holder = new ViewHolder(convertView);
            holder.tvXinwenfenlei = (TextView) convertView.findViewById(R.id.tv_xinwenfenlei);
            holder.viewXinwenfenleiDivider = (View) convertView.findViewById(R.id.view_xinwenfenlei_divider);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (listData != null) {
            if (position == listData.size() - 1) {
                holder.viewXinwenfenleiDivider.setVisibility(View.INVISIBLE);
            } else {
                holder.viewXinwenfenleiDivider.setVisibility(View.VISIBLE);
            }
            // 名称
            if (!TextUtils.isEmpty(listData.get(position).val)) {
                holder.tvXinwenfenlei.setText(listData.get(position).val);
            } else {
                holder.tvXinwenfenlei.setText("");
            }
            //是否被选中
            if (listData.get(position).isSelected) {
                holder.tvXinwenfenlei.setTextColor(context.getResources().getColor(R.color.common_green));
            } else {
                holder.tvXinwenfenlei.setTextColor(context.getResources().getColor(R.color.common_other));
            }
        }
        return convertView;
    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.view_xinwenfenlei_divider)
        View viewXinwenfenleiDivider;
        @BindView(R.id.tv_xinwenfenlei)
        TextView tvXinwenfenlei;
        @BindView(R.id.ll_xinwenfenlei)
        LinearLayout llXinwenfenlei;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
